<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends VController
{
    public function index()
    {

//        $qzone = I('session.qzone', '')['home'];
//        $say = M('say');
//        $data = $say->table('qzone_user qu,qzone_say qs')->field('qu.username,qs.content,qu.nickname,qs.ctime')->where("qu.id=qs.uid  AND qs.uid='{$qzone['id']}'")->select();
//        $Model = M('User');
//        $modelData = $Model->field('qzone_user.id,qzone_user.nickname,qzone_say.ctime,qzone_say_photo.path,qzone_say.content')->join('qzone_say ON qzone_say.uid = qzone_user.id', 'LEFT')->join('qzone_say_photo ON qzone_say.id=qzone_say_photo.say_id', 'LEFT')->where("qzone_user.id={$qzone['id']}")->select();
//        $saycount = $say->where("uid='{$_SESSION['qzone']['id']}'")->count();
//        $userData = $Model->field('qzone_user.id,qzone_user.nickname')->where("id='{$_SESSION['qzone']['id']}'")->select();
//        $list = M('user')->find($_SESSION['qzone']['id']);
        /*根据当前id的值来查询对应的数据，如果没有id则根据当前登录用户的id来查询出对应信息。如果查询不到数据也跳转到自己的主页。
         *如果是查看自己的空间则index控制器的index方法加载个人中心
         * 如果是查看别人的空间则加载个人主页
        */
//        现在不实现这个功能了
//        if($this->isMy)
//        {
//            //访问自己的个人中心，进入index控制器的index方法就只访问这个，如果自己需要访问自己的主页，则访问index控制器的main方法
//            $this->display('Index:infocent');
//            return false;
//        }
        //主页也是查看的自己的动态，与别人无关
        $Dynamic = D("Dynamic");
        $dynamics = $Dynamic->relation(true)->order('ctime DESC')->field('qd.*,qu.username,qu.zonedp')->table('qzone_user qu,qzone_dynamic qd')->where("uid='{$this->userData['id']}' AND qu.id=qd.uid")->select();
        $photoNum = M('AlbumPhoto')->where("uid='{$this->userData['id']}'")->count();
        $photoNum += M('SayPhoto')->where("uid='{$this->userData['id']}'")->count();
        $sayNum = M('Say')->where("uid='{$this->userData['id']}'")->count();
        $logNum = M('Log')->where("uid='{$this->userData['id']}'")->count();
        $info = M('User')->find($this->userData['id']);
        $message = D("Message");
        $messageData = $message->field('qm.*,qu.zonedp')->table('qzone_user qu,qzone_message qm')->relation(true)->where("who_id={$this->userData['id']} AND qm.uid=qu.id")->select();
        $this->assign('myUserData', $this->myUserData);
        $this->assign('userData', $this->userData);
        $this->assign('isMy', $this->isMy);
        $this->assign('dynamics', $dynamics);
        $this->assign('photoNum', $photoNum);
        $this->assign('sayNum', $sayNum);
        $this->assign('logNum', $logNum);
        $this->assign('info', $info);
        $this->assign('messageData', $messageData);
        $this->display();
    }
    //个人中心
    public function infocent()
    {
        //查看自己的动态
//        $model = M('Dynamic');
//        $dynamics = $model->where("uid='{$this->myUserData['id']}'")->order('ctime DESC')->select();
        $seeType = I('get.seeType','0');
        $uidArr = '';
        //个人中心可查看的动态有，0：好友动态，1：特别关心，2，与我相关(现在相当于我的动态)，3：那年今日，4：时光軕
        if($seeType == '0')
        {
            $tmp = M('Friends')->where("uid='{$this->myUserData['id']}'")->select();
            foreach ($tmp as $k=>$v)
            {
                if($k+1<count($tmp))
                {
                    $uidArr .= $v['who_id'].',';
                }else
                {
                    $uidArr .= $v['who_id'];
                }

            }
            if($tmp == '')
            {
                $uidArr = '0';
            }
            if(count($tmp)<=0)
            {
                $uidArr = '0';
            }
        }elseif ($seeType == '1')
        {
            //特别关心
            $tmp = M('Friends')->where("uid='{$this->myUserData['id']}' AND is_attention=1")->select();
            foreach ($tmp as $k=>$v)
            {
                if($k+1<count($tmp))
                {
                    $uidArr .= $v['who_id'].',';
                }else
                {
                    $uidArr .= $v['who_id'];
                }
            }
            if(count($tmp)<=0)
            {
                $uidArr = '0';
            }
        }elseif ($seeType == '2')
        {
            $uidArr = $this->myUserData['id'];
        }
        $Dynamic = D("Dynamic");
        $dynamics = $Dynamic->relation(true)->order('ctime DESC')->field('qd.*,qu.username,qu.zonedp')->table('qzone_user qu,qzone_dynamic qd')->where("uid in (".$uidArr.") AND qu.id=qd.uid")->select();
        $this->assign('myUserData', $this->myUserData);
        $this->assign('userData', $this->userData);
        $this->assign('isMy', $this->isMy);
        $this->assign('dynamics', $dynamics);
        $this->assign('seeType', $seeType);
        $this->display();
    }
    public function tmp()
    {
        $this->display();
    }


}
